package com.example.fbi_text.Acitivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fbi_text.R;
import com.example.fbi_text.database.DBHelper;

//修改密码界面
public class ChangePasswordActivity extends AppCompatActivity {
    private Button msure_button;
    private EditText mnew_password;
    private EditText mconfirm_password;
    // 在Activity中增加标志位记录状态
    private boolean isPasswordVisible = false;
    private int User_id;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        // 获取传递过来的账户ID
        User_id = getIntent().getIntExtra("User_id", -1);
        if (User_id == -1) {
            Toast.makeText(this, "无效的账户", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        msure_button=findViewById(R.id.sure_button);
        mnew_password = findViewById(R.id.new_password);
        mconfirm_password = findViewById(R.id.confirm_password);

        // 设置密码可见性切换
        setupPasswordToggle(mnew_password);

        msure_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPassword = mnew_password.getText().toString().trim();
                String confirmPassword = mconfirm_password.getText().toString().trim();

                // 验证输入
                if (newPassword.isEmpty()) {
                    Toast.makeText(ChangePasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!newPassword.equals(confirmPassword)) {
                    Toast.makeText(ChangePasswordActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 更新数据库中的密码
                boolean isUpdated = DBHelper.getInstance(ChangePasswordActivity.this)
                        .updateUserPassword(User_id, newPassword);

                if (isUpdated) {
                    Toast.makeText(ChangePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                } else {
                    Toast.makeText(ChangePasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    /**
     * 设置密码可见性切换功能
     */
    private void setupPasswordToggle(EditText editText) {
        editText.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Drawable[] drawables = editText.getCompoundDrawables();
                Drawable drawableEnd = drawables[2];

                if (drawableEnd != null) {
                    // 计算点击区域
                    int clickX = (int) event.getX();
                    int viewWidth = editText.getWidth();
                    int drawableWidth = drawableEnd.getIntrinsicWidth();
                    int paddingEnd = editText.getPaddingEnd();

                    // 检查是否点击了图标区域
                    if (clickX >= (viewWidth - drawableWidth - paddingEnd)) {
                        togglePasswordVisibility(editText);
                        return true; // 表示已处理该事件
                    }
                }
            }
            return false; // 其他区域点击继续由EditText处理
        });
    }

    private void togglePasswordVisibility(EditText editText) {
        try {
            int selection = editText.getSelectionEnd();

            if (!isPasswordVisible) {
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility, 0);
                isPasswordVisible = true;
            } else {
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off, 0);
                isPasswordVisible = false;
            }

            editText.setSelection(selection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
